package cn.com.captureNetPacket.util;

import java.io.UnsupportedEncodingException;
import java.time.LocalDateTime;
import java.util.Map;

import org.springframework.boot.json.JsonParseException;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;

import cn.com.captureNetPacket.CaptureNetPacketService;
import cn.com.captureNetPacket.model.DeviceStatus;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class RedisProxyMessageListener implements MessageListener{

	@Override
	public void onMessage(Message message, byte[] pattern) {
		String patternString="";
		 try {
			 if(pattern!=null)
			  patternString=new String(pattern,"UTF-8");
			 
		} catch (UnsupportedEncodingException e) {
			log.error("onMessage ",e);
		} 
		 DeviceStatus deviceStatus=null;
		 try {
		 log.info("收到redis 订阅消息：{}", message,patternString);
		 Map <String,Object> mesSta=JsonPathUtil.getValue(message.toString(), "$",Map.class);
		 String callId=null,caller=null,called=null,thisDn=null,agentId;
		 
		 
		 if(mesSta.containsKey("stateid"))
		 {
			 thisDn=mesSta.get("thisdn").toString();
			 deviceStatus= CaptureNetPacketService.getDeviceStatus().get(thisDn);
			  
			 // 振铃
			if(mesSta.get("stateid").toString().equals("EventRinging")) {
				if(deviceStatus==null) {
					deviceStatus=new DeviceStatus();
				}
				callId=mesSta.get("connid").toString();
				caller=mesSta.get("otherdn").toString();
				agentId=mesSta.get("agentid").toString();
				deviceStatus.setAgentID(agentId);
				deviceStatus.setCallUUID(callId);
				deviceStatus.setCaller(caller);
				deviceStatus.setCalled(thisDn);
				deviceStatus.setDeviceNumber(thisDn);
				deviceStatus.setRingTime(LocalDateTime.now());
				deviceStatus.setDirection("in");
				 CaptureNetPacketService.getDeviceStatus().put(thisDn, deviceStatus);
			}else if (mesSta.get("stateid").toString().equals("EventDialing")) {
				if(deviceStatus==null) {
					deviceStatus=new DeviceStatus();
				}
				callId=mesSta.get("connid").toString();
				called=mesSta.get("otherdn").toString();
				agentId=mesSta.get("agentid").toString();
				deviceStatus.setAgentID(agentId);
				deviceStatus.setCallUUID(callId);
				deviceStatus.setCalled(called);
				deviceStatus.setCaller(thisDn);
				deviceStatus.setDeviceNumber(thisDn);
				deviceStatus.setRingTime(LocalDateTime.now());
				deviceStatus.setDirection("out");
				CaptureNetPacketService.getDeviceStatus().put(thisDn, deviceStatus);
			}else if (mesSta.get("stateid").toString().equals("EventEstablished")) {
				if(deviceStatus==null)return ;
				if(deviceStatus.getEstablishTime()==null)
				deviceStatus.setEstablishTime(LocalDateTime.now());
				
			}else if (mesSta.get("stateid").toString().equals("EventReleased")) {
				if(deviceStatus==null)return ;
				if(mesSta.get("reason").toString().trim().length()>0) {
					if(deviceStatus.getEndTime()==null){
						deviceStatus.setEndTime(LocalDateTime.now());
				     }
				}
				if( CaptureNetPacketService.getDeviceStatus().get(thisDn)!=null) {
					 CaptureNetPacketService.getDeviceStatus().remove(thisDn);
				}
				
			}
			
			
			
			
		 }
		 
		 }catch (JsonParseException e){
			 log.warn("onMessage {} ,错误信息 json转换错误: {}",message,e.getMessage());
		 }
		 catch (Exception e) {
			 log.error("onMessage {} ,错误信息: {}",message,e.getMessage());
		}
	}

}
